import java.util.Objects;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/7/19 13:22
 */
class Money implements Cloneable{//实现克隆时，一定要实现Cloneable接口，然后重写clone方法
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}




class Person implements Cloneable{
    public String name;
    public int age;
    public Money money = new Money();
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {//可能会抛出不支持克隆异常
        Person tmp = (Person)super.clone();
        tmp.money = (Money)this.money.clone();
        return tmp;
    }

    /*@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }
*/
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    /* @Override
    public boolean equals(Object obj) {//重写equal方法
        Person tm = (Person)obj;//obj不一定是Person类，而已知引用一定是Person类，所以要强转
        return this.name == tm.name && this.age == tm.age;
    }*/
}




interface Ia{
    public static final int a = 10;
    public abstract void darw();
}
public class Test {
    public static void main(String[] args) {
        Person person = new Person("zhangsan",19);
        Person person1 = new Person("zhangsan",19);
        System.out.println(person.equals(person1));
    }










    public static void main1(String[] args) throws CloneNotSupportedException{//声明异常
        Person person = new Person("zhangsan",19);
        Person person1 =(Person)person.clone();//clone方法返回的类型是object类，所以要进行强转
        System.out.println(person.money.m);
        System.out.println(person1.money.m);
        System.out.println("============");
        person.money.m = 99.99;
        System.out.println(person.money.m);
        System.out.println(person1.money.m);

    }
}
